﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class WorkOrderEquipListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }
        protected void btnReturnEquip_OnClick(Object sender, EventArgs e)
        {
            //GridView gv = upnlWorkOrderEquipListControl.FindControl("gvWorkOrderEquipList") as GridView;
            //if (gv != null)
            //{
                TextBox txtReturnQty = gvWorkOrderEquipList.SelectedRow.FindControl("txtReturnQty") as TextBox;
                //TextBox txtReturnQty = gv.SelectedRow.FindControl("txtReturnQty") as TextBox;

                if (txtReturnQty != null && txtReturnQty.Text.Length > 0 && RSSBMCommonFunctions.IsNumeric(txtReturnQty.Text) == true)
                {
                    RSUserIdentity userIdentity = GetRSUserIdentity();
                    int companyid = userIdentity.RSCompanyId;
                    int retqty = Convert.ToInt32(txtReturnQty.Text);
                    int WorkOrderproductid = Convert.ToInt32(gvWorkOrderEquipList.SelectedValue.ToString());
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnQty", SqlDbType.Int, 0, ParameterDirection.Input, retqty);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderProductId", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderproductid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "UPDATE rs_tblWorkOrderEquip SET ReturnQty=@ReturnQty WHERE WorkOrderProductId=@WorkOrderProductId AND CompanyId=@CompanyId");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int WorkOrderid = Convert.ToInt32(hfWorkOrderId.Value);
                    LoadWorkOrderEquipListByID(companyid, WorkOrderid);
                    //SetSelectedRowState(false);
                    //gvWorkOrderEquipList.SelectedIndex = -1;
                }
            //}
        }

        private void SetSelectedRowState(bool bstate)
        {
            if (gvWorkOrderEquipList.SelectedIndex >= 0)
            {
                TextBox txtReturnQty = gvWorkOrderEquipList.SelectedRow.FindControl("txtReturnQty") as TextBox;
                if (txtReturnQty != null)
                {
                    txtReturnQty.Enabled = bstate;
                    if (bstate == false)
                        txtReturnQty.BackColor = System.Drawing.Color.Gainsboro;
                    else
                        txtReturnQty.BackColor = System.Drawing.Color.White;

                    Button btnAdd = gvWorkOrderEquipList.SelectedRow.FindControl("btnReturnEquip") as Button;
                    if (btnAdd != null)
                        btnAdd.Visible = bstate;
                }
            }
        }
        protected void gvWorkOrderEquipList_SelectedIndexChanging(Object sender, EventArgs e)
        {
            SetSelectedRowState(false);
        }
        protected void gvWorkOrderEquipList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            SetSelectedRowState(true);
        }

        public void LoadWorkOrderEquipListByID(int companyid, int WorkOrderid)
        {
            hfWorkOrderId.Value = WorkOrderid.ToString();
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select pe.WorkOrderproductid, pe.WorkOrderid, pe.productid, pe.quantity, pe.isreturned, pe.returnqty, i.productname, i.unitsinstock, i.ModelNo from rs_tblWorkOrderEquip as pe left join rs_tblInventory i on pe.ProductId=i.productid where pe.workorderid=@WorkOrderid and pe.companyid=@companyid order by productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderEquipList.DataSource = ds;
            gvWorkOrderEquipList.DataBind();
            myConnection.Close();

        }

        protected void gvWorkOrderEquipList_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderproductid = Convert.ToInt32(gvWorkOrderEquipList.DataKeys[e.RowIndex].Value);
                RemoveWorkOrder(companyid, WorkOrderproductid);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private void RemoveWorkOrder(int companyid, int WorkOrderproductid)
        {
            if (companyid > 0 && WorkOrderproductid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderproductid", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderproductid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrderEquip WHERE WorkOrderproductid=@WorkOrderproductid AND companyid=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                string prevurl = Request.RawUrl;
                //if (!prevurl.Contains("tabindex="))
                //    prevurl = prevurl + "&tabindex=2";
                Response.Redirect(prevurl);
            }
        }

        protected void gvProduct_Edit(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int productid = Convert.ToInt32(e.CommandArgument.ToString());
                ucModalProductControl.ShowModal(companyid, productid, true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
    }

    
}